package cn.lili.controller.client;

import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.CommonUtil;
import cn.lili.modules.file.client.UploadClient;
import cn.lili.modules.file.entity.File;
import cn.lili.modules.file.entity.dto.FileUploadDTO;
import cn.lili.modules.file.plugin.FilePluginFactory;
import cn.lili.modules.file.service.FileService;
import cn.lili.modules.system.client.SettingClient;
import com.aliyun.oss.model.PutObjectResult;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.net.URL;
import java.util.Objects;

@RestController
@RequiredArgsConstructor
public class FileFeignController implements UploadClient {

    @Autowired
    private FilePluginFactory filePluginFactory;

    private final FileService fileService;

    @Override
    public String upload(FileUploadDTO fileUploadDTO) {
        return filePluginFactory.filePlugin().inputStreamUpload(fileUploadDTO.getBucketName(), fileUploadDTO.getContext(), fileUploadDTO.getKey());
    }

    /**
     * 将网络图片转存到oss中
     * @param url
     * @return
     */
    @Override
    public String upload(String url) {
        System.out.println("====================image_url===="+url);
        AuthUser authUser = UserContext.getCurrentUser();
        if(null == authUser || StringUtil.isEmpty(authUser.getUsername())){
            authUser = new AuthUser();
            authUser.setUsername("admin");
            authUser.setScene(SceneEnums.SYSTEM);
        }
        File newFile = new File();
        String result = null;
        try {
            InputStream inputStream = new URL(url).openStream();
            String fileName = url.substring(url.lastIndexOf("/")+1);
            if(fileName.indexOf("?")>-1){
                fileName = fileName.substring(0,fileName.indexOf("?"));
            }
            String fileKey = CommonUtil.rename(Objects.requireNonNull(fileName));
            //上传至第三方云服务或服务器
            result = filePluginFactory.filePlugin().inputStreamUpload(inputStream, fileKey);
            //保存数据信息至数据库
            newFile.setName(fileName);
            //newFile.setFileSize(file.getSize());
            //newFile.setFileType(file.getContentType());
            newFile.setFileKey(fileKey);
            newFile.setUrl(result);
            newFile.setCreateBy(authUser.getUsername());
            newFile.setUserEnums(authUser.getScene().name());
            //如果是店铺，则记录店铺id
            if (Objects.requireNonNull(authUser.getScene()) == SceneEnums.MANAGER) {
                newFile.setOwnerId("-1");
            } else {
                newFile.setOwnerId(authUser.getExtendId());
            }
            fileService.save(newFile);
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }
}
